package com.neuronbit.businessflow.flow.source;

import com.neuronbit.businessflow.flow.clazz.ClassInfo;
import com.neuronbit.businessflow.flow.clazz.MethodInfo;
import com.neuronbit.businessflow.flow.source.util.SourceContext;
import com.neuronbit.businessflow.intellij.model.*;
import com.neuronbit.javaparser.ast.NodeList;
import com.neuronbit.javaparser.ast.body.Parameter;
import com.neuronbit.javaparser.ast.stmt.BlockStmt;
import com.neuronbit.javaparser.ast.stmt.CatchClause;
import com.neuronbit.javaparser.ast.stmt.TryStmt;
import com.neuronbit.javaparser.ast.type.ClassOrInterfaceType;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

import static java.lang.String.format;

/**
 * The type User task step java source.
 */
public abstract class UserTaskStepJavaSource extends StepJavaSource {
    /**
     * Instantiates a new Abstract node source generator.
     *
     * @param node          the node
     * @param sourceContext the source context
     */
    public UserTaskStepJavaSource(Node node, SourceContext sourceContext) {
        super(node, sourceContext);
    }

    /**
     * Prepare step params list.
     *
     * @param methodBody the method body
     * @return the list
     */
    protected List<String> prepareStepParams(BlockStmt methodBody) {
        Collection<Param> inputs = ((JavaMethodTask) getNode()).getCallable().getParams().values();
        List<String> paramNames = new ArrayList<>();
        if (null != inputs) {
            for (Param input : inputs) {
                if (null == input) {
                    continue;
                }
                //TODO shunyun 2024/1/17: defaultValue
                ParamValue value = input.getValue();
                if (null == value) {
                    continue;
                }
                String ex = processValueSource(value, getFlowNodeSource());
                if (StringUtils.isNotBlank(ex)) {
                    addImport(input.getType());
                    methodBody.addStatement(format("%s %s = %s;",
                            input.getType().getName(),
                            input.getName(),
                            ex
                    ));
                    paramNames.add(input.getName());
                }
            }
        }

        return paramNames;
    }
}
